﻿using System;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Factory;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;

namespace Me.Prettyprint.Cassandra.Model.Thrift
{
    /// <summary>
    /// A query for the thrift call get_range_slices of supercolumns
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="SN"></typeparam>
    /// <typeparam name="N"></typeparam>
    /// <typeparam name="V"></typeparam>
    public class ThriftRangeSuperSlicesQuery<K, SN, N, V> : AbstractSliceQuery<K, SN, V, OrderedSuperRows<K, SN, N, V>>, RangeSuperSlicesQuery<K, SN, N, V>
    {
        public ThriftRangeSuperSlicesQuery(Keyspace keyspace, Serializer<K> keySerializer,
            Serializer<SN> sNameSerializer, Serializer<N> nameSerializer, Serializer<V> valueSerializer) :
            base(keyspace, keySerializer, sNameSerializer, valueSerializer)
        {
            if (nameSerializer == null)
            {
                throw new NullReferenceException("NameSerializer name cannot be null");
            }
            NameSerializer = nameSerializer;
            KeyRange = new HKeyRange<K>(keySerializer);
        }
        protected Serializer<N> NameSerializer
        {
            get;set;
        }
        public HKeyRange<K> KeyRange
        {
            get;
            set;
        }
        public RangeSuperSlicesQuery<K, SN, N, V> SetKeys(K start, K end)
        {
            KeyRange.SetKeys(start, end);
            return this;
        }
        public RangeSuperSlicesQuery<K, SN, N, V> SetRowCount(int rowCount)
        {
            KeyRange.RowCount = rowCount;
            return this;
        }
        public override string ToString()
        {
            return "RangeSuperSlicesQuery(" + KeyRange + base.ToStringInternal() + ")";
        }
        public new RangeSuperSlicesQuery<K, SN, N, V> SetColumnNames(List<SN> columnNames)
        {
            return (RangeSuperSlicesQuery<K, SN, N, V>) base.SetColumnNames(columnNames);
        }
        public new RangeSuperSlicesQuery<K, SN, N, V> SetRange(SN start, SN finish, bool reversed, int count)
        {
            return (RangeSuperSlicesQuery<K, SN, N, V>) base.SetRange(start, finish, reversed, count);
        }
        public new RangeSuperSlicesQuery<K, SN, N, V> SetColumnFamily(string cf)
        {
            return (RangeSuperSlicesQuery<K, SN, N, V>) base.SetColumnFamily(cf);
        }
        public override QueryResult<OrderedSuperRows<K,SN,N, V>> Execute()
        {
            if (ColumnFamilyName == null)
            {
                throw new NullReferenceException("ColumnFamily name cannot be null");
            }
            Func<KeyspaceService, OrderedSuperRows<K,SN,N,V>> x = (KeyspaceService ks) =>
            {
                ColumnParent columnParent = new ColumnParent(ColumnFamilyName);
                Dictionary<K, List<SuperColumn>> thriftRet = KeySerializer.FromBytesDictionary(
                    ks.GetSuperRangeSlices(columnParent, GetPredicate(), KeyRange.ToThrift()));
                return new OrderedSuperRowsImpl<K,SN,N,V>(thriftRet, KeySerializer,
                    ColumnNameSerializer, NameSerializer, ValueSerializer);
            };
            return new QueryResultImpl<OrderedSuperRows<K,SN,N,V>>(
                Keyspace.DoExecute(new KeyspaceOperationCallbackBuilder<OrderedSuperRows<K,SN,N,V>>(x)), this);
        }
    }
}
